package com.bw.BaseImage.impl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.RoundedCorner;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bw.BaseImage.IImageLoader;
import com.bw.BaseImage.common.ImageOption;


public class GlideImpl implements IImageLoader {
    @Override
    public void loadImage(Context context, String url, ImageView view, ImageOption option) {
        this.load(context,url,view,option);
    }

    @Override
    public void loadImage(Context context, Drawable drawable, ImageView view, ImageOption option) {
        this.load(context,drawable,view,option);
    }

    @Override
    public void loadImage(Context context, Bitmap bitmap, ImageView view, ImageOption option) {
        this.load(context,bitmap,view,option);
    }

    @Override
    public void loadImage(Context context, byte[] content, ImageView view, ImageOption option) {
        this.load(context,content,view,option);
    }

    @Override
    public void loadImageCircle(Context context, String url, ImageView view, ImageOption option) {
        RequestBuilder<Drawable> builder = Glide.with(context).load(url).circleCrop();
        if(option!=null){
            if(option.getPlaceImage()!=null){
                builder.placeholder(option.getPlaceImage());
            }
            if(option.getErrorImage()!=null){
                builder.error(option.getErrorImage());
            }
            builder.skipMemoryCache(option.isSkipMemoryCache());
            if(option.isSkipDiskCache()){
                builder.diskCacheStrategy(DiskCacheStrategy.NONE);
            }
        }
        builder.into(view);
    }

    @Override
    public void loadImageCircle(Context context, Drawable drawable, ImageView view, ImageOption option) {
        RequestBuilder<Drawable> builder = Glide.with(context).load(drawable).circleCrop();
        if(option!=null){
            if(option.getPlaceImage()!=null){
                builder.placeholder(option.getPlaceImage());
            }
            if(option.getErrorImage()!=null){
                builder.error(option.getErrorImage());
            }
            builder.skipMemoryCache(option.isSkipMemoryCache());
            if(option.isSkipDiskCache()){
                builder.diskCacheStrategy(DiskCacheStrategy.NONE);
            }
        }
        builder.into(view);
    }

    @Override
    public void loadImageRouter(Context context, String url, ImageView view, ImageOption option, Integer radian) {
        RequestBuilder<Drawable> builder = Glide.with(context).load(url).transform(new RoundedCorners(radian));
        if(option!=null){
            if(option.getPlaceImage()!=null){
                builder.placeholder(option.getPlaceImage());
            }
            if(option.getErrorImage()!=null){
                builder.error(option.getErrorImage());
            }
            builder.skipMemoryCache(option.isSkipMemoryCache());
            if(option.isSkipDiskCache()){
                builder.diskCacheStrategy(DiskCacheStrategy.NONE);
            }
        }
        builder.into(view);
    }

    private void load(Context context,Object content,ImageView view,ImageOption option){
        RequestBuilder<Drawable> builder = Glide.with(context).load(content);
        if(option!=null){
            if(option.getPlaceImage()!=null){

                builder.placeholder(option.getPlaceImage());
            }
            if(option.getErrorImage()!=null){
                builder.error(option.getErrorImage());
            }
            builder.skipMemoryCache(option.isSkipMemoryCache());
            if(option.isSkipDiskCache()){
                builder.diskCacheStrategy(DiskCacheStrategy.NONE);
            }
        }
        builder.into(view);
    }


}
